package com.asm.controller;

import com.asm.dto.Result;
import org.springframework.boot.autoconfigure.web.servlet.error.AbstractErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author ZhuangGuiMan
 */
@RestController
public class SystemErrorController extends AbstractErrorController {

    private final String PATH = "/error";

    public SystemErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @RequestMapping("/error")
    public ResponseEntity<Result> error(HttpServletRequest request) {
        Map<String, Object> attributes = getErrorAttributes(request, true);
        return new ResponseEntity<>(new Result(getStatus(request), (String) attributes.get("message"), null), getStatus(request));
    }

    @Override
    public String getErrorPath() {
        return PATH;
    }

}
